/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package negocio;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import persistencia.dao.hibernate.ClienteDAOImpl;
import persistencia.pojo.Cliente;
import persistencia.util.HibernateUtil;

/**
 *
 * @author Victor
 */
public class ClienteManager {
    private ClienteDAOImpl mgr;
    private Session session;

    public ClienteManager() {
        mgr = new ClienteDAOImpl();
        session = HibernateUtil.getSessionFactory().openSession();
    }
    public void closeSesion(){
        session.close();
    }
    public void saveCliente(Cliente cliente){
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            mgr.saveCliente(session, cliente);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
    }
    public void deleteCliente(Cliente cliente){
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            mgr.deleteCliente(session, cliente);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
    }
    public void updateCliente(Cliente cliente){
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            mgr.saveCliente(session, cliente);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
    }

    public List<Cliente> getAllClientes(){
        Transaction tx = null;
        List<Cliente> retorno = null;
        try {
            tx = session.beginTransaction();
            retorno = mgr.getAllClientes(session);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
        return retorno;
    }
    public List<Cliente> getClientesWhere(String strColTabla, String strValueCol, String strPagActual){
        Transaction tx = null;
        List<Cliente> retorno = null;
        try {
            tx = session.beginTransaction();
            retorno = mgr.getClientesWhere(session, strColTabla, strValueCol, strPagActual);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
        return retorno;
    }
    public Cliente getCliente(String codcliente){
        Transaction tx = null;
        Cliente retorno = null;
        try {
            tx = session.beginTransaction();
            retorno = mgr.getCliente(session,codcliente);
            tx.commit();
        } catch (Exception e) {
            tx.commit();
        } finally{
            //session.close();
        }
        return retorno;
    }
}
